/**
 * 广告支出-按行业属性统计(按条查询)
 */    
 function GetRequest() {
    var url = decodeURI(location.search);
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return theRequest;
}  
  
$(function(){
	var page;
	var object = GetRequest();
	init(1,null);
	function init(page,byindustry){
		var initTable="<tr>" +
				"<th>序号</th>" +
				"<th>时间</th>" +
				"<th>行业属性</th>" +
				"<th>广告数目</th>" +
				"<th>平均单价</th>" +
				"<th>点击次数</th>" +
				"<th>广告支出</th>" +
				"<th>支出/总支出</th>" +
				"<th>总广告预算</th>" +
				"<th>支出排名</th>" +
				"</tr>	";
		$('#ByAdIndustryOneday').html(initTable);
		if(page==null || page.length<=0){
			page=1;
		}
		var endTime = getQueryString("endTime"); 
		var startTime=getQueryString("startTime");
		var byindustry=object.byindustry;
		
		$("#endTime").val(endTime);
		$("#startTime").val(startTime);
		$("#byindustry").val(object.byindustry);
		if(endTime&&startTime){
			$.ajax({
				url : '/web-adv/byIndustry',
		        data:{"startTime":startTime,"endTime":endTime,"byindustry":byindustry,"page":page},
		        dataType:'json',
				type : 'post',
				success : function(data) {
					$('#ByAdIndustryOneday').find("tr").eq(0).siblings("tr").remove();
					var beans = data.beans;
					var adCount=1;
					var proportions=0;
					var recharges=0;
					var adBudgets=0;
					var MyPays=0;
					$('#pageBox').extendPagination({
						totalCount:data.total,
				 		showPage: data.pages+1,
			            limit: data.pageSize,
			            goPage:page,
			            callback: function (page) {
			            	Byindustry2(page);	
			            }
					});
					//循环输出表格
					for(var i = 0; i < beans.length;i++){
						MyPays+=parseInt(beans[i].adPay);
						
						var tbody = "<tr><td>"+(i+1)+"</td>" +
						"<td>"+beans[i].time +"</td>" +
						"<td>"+beans[i].adIndustry +"</td>" +
						"<td>"+adCount+"</td>" +
						"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
						"<td>"+beans[i].clickNum+"</td>" +
						"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
						"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
						"<td>"+beans[i].adBudget+"</td>" +
						"<td>"+((page-1)*5+(i+1))+"</td>"+
						"</tr>"
						
						$('#ByAdIndustryOneday').append(tbody);
						proportions+=beans[i].proportion*100;
						adBudgets+=beans[i].adBudget*1;
						clickNums=beans[i].clickNum*1;
					}
					var tbodyTwo="<tr>" +
					"<td>总计</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>---</td>" +
					"<td>"+clickNums+"</td>" +
					"<td>"+MyPays.toFixed(2)+"</td>" +
				    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
				    "<td>"+adBudgets.toFixed(2)+"</td>" +
				    "<td>---</td>" +
				 "</tr>";
					$('#ByAdIndustryOneday').append(tbodyTwo);
				},
		        error: function(){}        
			});
		}
	}
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					var byindustry = $("#byindustry").val();
					init(page,byindustry);
				}else if(page>showPageN-4){
					
				}else{
					total(page);
				}
			}
		}
		
		$("input[name='selectPage']").val("");
	});
	
});

function total(page){
	var startTime=GetRequest().startTime;
	var endTime=GetRequest().endTime;
	var byindustry=GetRequest().byindustry;
	if(endTime&&startTime){
		$.ajax({
			url : '/web-adv/byIndustry',
	        data:{"startTime":startTime,"endTime":endTime,"byindustry":byindustry,"page":page},
	        dataType:'json',
			type : 'post',
			success : function(data) {
				var beans = data.beans;
				var adCount=1;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var MyPays=0;
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	console.log(page);
		            	Byindustry2(page);	
		            }
				});
			},
	        error: function(){}        
		});
	 }
}

function Byindustry(startTime,endTime,byindustry){
	var page;
	if(page==null || page.length<=0){
		var page=1;
	}
	$.ajax({
		url : '/web-adv/byIndustry',
        data:{"startTime":startTime,"endTime":endTime,"page":page,"byindustry":byindustry},
        dataType:'json',
		type : 'post',
		success : function(data) {
			$('#ByAdIndustryOneday').find("tr").eq(0).siblings("tr").remove();
			var beans = data.beans;
			var adCount=1;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			var MyPays=0;
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	
	            	Byindustry2(page,startTime,endTime,byindustry);	
	            }
			});
			//循环输出表格
			for(var i = 0; i < beans.length;i++){
				MyPays+=parseInt(beans[i].adPay);
				
				var tbody = "<tr><td>"+(i+1)+"</td>" +
				"<td>"+beans[i].time +"</td>" +
				"<td>"+beans[i].adIndustry+"</td>" +
				"<td>"+adCount+"</td>" +
				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
				"<td>"+beans[i].clickNum+"</td>" +
				"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
				"<td>"+beans[i].adBudget+"</td>" +
				"<td>"+((page-1)*5+(i+1))+"</td>"+
				"</tr>"
				
				$('#ByAdIndustryOneday').append(tbody);
				proportions+=beans[i].proportion*100;
				adBudgets+=beans[i].adBudget*1;
				clickNums=beans[i].clickNum*1;
			}
			var tbodyTwo="<tr>" +
			"<td>总计</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>"+clickNums+"</td>" +
			"<td>"+MyPays.toFixed(2)+"</td>" +
		    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
		    "<td>"+adBudgets.toFixed(2)+"</td>" +
		    "<td>---</td>" +
		 "</tr>";
				$('#ByAdIndustryOneday').append(tbodyTwo);
		},
        error: function(){}        
	});
}



function Byindustry2(page,startTime,endTime,byindustry){
	/*var object = GetRequest();
	var endTime = $("#endTime").val(),startTime=$("#startTime").val();
	var byindustry=object.byindustry;*/
	$("#endTime").val(endTime);
	$("#startTime").val(startTime);
	$("#byindustry").val(byindustry);
	if(endTime&&startTime){
	$.ajax({
		url : '/web-adv/byIndustry',
        data:{"startTime":startTime,"endTime":endTime,"byindustry":byindustry,"page":page},
        dataType:'json',
		type : 'post',
		success : function(data) {
			$('#ByAdIndustryOneday').find("tr").eq(0).siblings("tr").remove();
			var beans = data.beans;
			var adCount=1;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			//总支出
			var sumPay = data.sumPay;
			var MyPays=0;
			//循环输出表格
			for(var i = 0; i < beans.length;i++){
				MyPays+=parseInt(beans[i].adPay);
				
				var tbody = "<tr><td>"+(i+1)+"</td>" +
				"<td>"+beans[i].time +"</td>" +
				"<td>"+beans[i].adIndustry +"</td>" +
				"<td>"+adCount+"</td>" +
				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
				"<td>"+beans[i].clickNum+"</td>" +
				"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
				"<td>"+beans[i].adBudget+"</td>" +
				"<td>"+((page-1)*5+(i+1))+"</td>"+
				"</tr>"
				
				$('#ByAdIndustryOneday').append(tbody);
				proportions+=beans[i].proportion*100;
				adBudgets+=beans[i].adBudget*1;
				clickNums=beans[i].clickNum*1;
			}
			var tbodyTwo="<tr>" +
			"<td>总计</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>"+clickNums+"</td>" +
			"<td>"+MyPays.toFixed(2)+"</td>" +
		    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
		    "<td>"+adBudgets.toFixed(2)+"</td>" +
		    "<td>---</td>" +
		 "</tr>";
				$('#ByAdIndustryOneday').append(tbodyTwo);
		},
        error: function(){}        
	});
  }
}
